package org.example.publisherrealtime.service.impl;

import org.example.publisherrealtime.bean.NameValue;
import org.example.publisherrealtime.mapper.PublisherMapper;
import org.example.publisherrealtime.service.PublisherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PublisherServiceImpl implements PublisherService {

    @Autowired
    PublisherMapper publisherMapper ;


    @Override
    public Map<String, Object> doDetailByItem(String itemName, String date, Integer pageNo, Integer pageSize) {
        Integer from = (pageNo - 1) * pageSize;
        Map<String, Object> results = publisherMapper.searchDetailByItem(itemName,date,from,pageSize);
        return results;
    }

    @Override
    public List<NameValue> dostatByItem(String itemName, String date, String t) {
        List<NameValue> searchResult = publisherMapper.searchStatByItem(itemName,date,typeToField(t));
        return transForm(searchResult,t);
    }

//转换数据格式
    @Override
    public List<NameValue> transForm(List<NameValue> searchResult, String t){
        //直接对数据做判断进行转换
        if("gender".equals(t)){
            //数据不为空
            if(searchResult.size()>0){
                //进行循环遍历
                for(NameValue nameValue:searchResult){
                    String name = nameValue.getName();
                    if("F".equals(name)){
                        nameValue.setName("女");
                    }
                    if("M".equals(name)){
                        nameValue.setName("男");
                    }
                }
            }
            return searchResult;
        } else if ("age".equals(t)) {
            if(searchResult.size()>0){
                double totalammount20 =  0;
                double totalammount20to29 =  0;
                double totalammount30 =  0;
                for(NameValue nameValue:searchResult){
                    int age = Integer.parseInt(nameValue.getName());
                    double totalammount = Double.parseDouble(nameValue.getValue().toString());
                    if(age<20){
                        totalammount20+=totalammount;
                    }else if(age>20 && age<30){
                        totalammount20to29+=totalammount;
                    }else {
                        totalammount30+=totalammount;
                    }

                }
                searchResult.clear();
                searchResult.add(new NameValue("20岁以下",totalammount20));
                searchResult.add(new NameValue("20到30岁",totalammount20to29));
                searchResult.add(new NameValue("30岁及以上",totalammount30));
            }
            return searchResult;
        }
        return searchResult;
    }


    public String typeToField(String t){
        if("age".equals(t)){
            return "user_age";
        } else if ("gender".equals(t)) {
            return "user_gender";
        }
        return null;
    }


    public Map<String, Object> doDauRealtime(String td){
        //方法具体实现在类里面
       //首先接收数据，然后将去数据层进行处理

        Map<String,Object> dauInfo = publisherMapper.searchDau(td);
        return dauInfo;

    }

}
